package com.gcsj.controller;

import com.alibaba.fastjson.JSONObject;
import com.gcsj.pojo.User;
import com.gcsj.service.UserService;
import com.gcsj.vo.PageRequest;
import com.gcsj.vo.PageResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;



    @ApiOperation(value = "通过id查询用户")//swagger接口信息备注
    @GetMapping("/getOne")
    public JSONObject getUser(@RequestParam Integer id) {
        JSONObject jsonObject = new JSONObject();
        User one = userService.getUserById(id);
        if(null != one) {
            jsonObject.put("code", 200);
            jsonObject.put("user", JSONObject.parseObject(one.toString()));
            jsonObject.put("msg", "更新成功!");
        }
        else {
            jsonObject.put("code", 201);
            jsonObject.put("msg", "更新失败!");
        }
//        System.out.println("getone");
        return jsonObject;
    }

    @ApiOperation(value = "用户注册，新增")//swagger接口信息备注
    @PostMapping("/register")
    public JSONObject register(@RequestBody User user){//post专用
        JSONObject jsonObject = new JSONObject();
//        System.out.println(user);
        if(user.getNickName() != null && user.getPassword() != null) {

            //通过用户名查询该用户
            User one = userService.getUserByName(user.getNickName());
            System.out.println(one);
            //用户存在
            if(one != null) {
                jsonObject.put("code",201);
                jsonObject.put("msg","注册失败，用户名已存在");
                return jsonObject;
            }
            else
            {
                user.setStatus(0);
                user.setRole(0);
                int check = userService.addUser(user);
                System.out.println(check);
                if(check==1) {
                    jsonObject.put("code", 200);
                    jsonObject.put("msg","注册成功");
                    return jsonObject;
                }
            }
        }

        jsonObject.put("code", 202);
        jsonObject.put("msg", "其他错误");
        return jsonObject;
    }

    @ApiOperation(value="登入")
    @PostMapping("/login")
    public JSONObject login(@RequestBody User user){
        JSONObject jsonObject = new JSONObject();
//        System.out.println(user);

        //用户名密码不为空
        if(user.getNickName() != null && user.getPassword() != null) {

            //通过用户名查询该用户
            User one = userService.getUserByName(user.getNickName());

            //用户存在 用户没有注销
            if(one != null && one.getStatus() == 0) {
//                System.out.println(one.toString());

                //用户名,密码正确
                if(one.getNickName().equals(user.getNickName())  && one.getPassword().equals(user.getPassword())) {
                    jsonObject.put("code", 200);
                    jsonObject.put("user", JSONObject.parseObject(one.toString()));
                    jsonObject.put("msg", "登入成功!");
                    return jsonObject;
                }
            }
        }
        //其他各种错误的处理
        jsonObject.put("code", 201);
        jsonObject.put("msg", "账户或密码错误!");
        return jsonObject;
    }

    @ApiOperation(value = "用户条件查询list")
    @GetMapping("/userList")
    public JSONObject getUserList(User user){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 200);
        return jsonObject;
    }

    @GetMapping("/admin")
    public JSONObject listUser(@RequestParam(name="pageNum") Integer pageNum,
                           @RequestParam(name="pageSize") Integer pageSize){
        JSONObject jsonObject = new JSONObject();
        PageRequest pageQuery = new PageRequest();
        pageQuery.setPageNum(pageNum);
        pageQuery.setPageSize(pageSize);
        PageResult pageResult = userService.listUser(pageQuery);
//        System.out.println(pageResult);
        jsonObject.put("data",pageResult);
//        System.out.println(jsonObject);
        return jsonObject;
    }

    @GetMapping("/selectUserListByPage")
    public Object getUserListByPage(@RequestParam(name="pageNum") Integer pageNum,
                                    @RequestParam(name="pageSize") Integer pageSize,
                                    User user) {
        PageRequest pageQuery1 = new PageRequest();
        pageQuery1.setPageNum(pageNum);
        pageQuery1.setPageSize(pageSize);

        return userService.selectUserListBy(pageQuery1,user);

    }


}
